<template>
  <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/customer' }" style="font-size:18px">顾客管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/change_password'}" style="font-size:18px">修改密码</el-breadcrumb-item>
      </el-breadcrumb>
      <div id="register">
        <p class="title">修改密码</p>
        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules2"
          ref="ruleForm2"
          label-width="0"
          class="demo-ruleForm"
        >
          <el-form-item prop="old_pass">
            <el-input style="width:80%;" v-model="ruleForm2.old_pass" auto-complete="off" placeholder="输入旧密码"></el-input>
          </el-form-item>
          <el-form-item prop="pass">
            <el-input style="width:80%;" type="password" v-model="ruleForm2.pass" auto-complete="off" placeholder="输入新密码"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <el-input style="width:80%;" type="password"  v-model="ruleForm2.checkPass" auto-complete="off" placeholder="确认新密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm2')" style="width:80%;">确认修改</el-button>
          </el-form-item>
        </el-form>
      </div>
</div>
</template>
<script> 
export default {
  name: "Register", 
  data() {
    let checkOld_pass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error('请输入旧密码'))
      } else {
        callback()
      }
    }
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"))
      } else if (value === this.ruleForm2.old_pass) {
        callback(new Error("新密码不能与原来相同!"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback()
      }
    }
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {  
      ruleForm2: {
        pass: "",
        checkPass: "",
        tel: ""
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: 'change' }],
        checkPass: [{ validator: validatePass2, trigger: 'change' }],
        old_pass: [{ validator: checkOld_pass, trigger: 'change' }],
      },
    }
  }, 
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$alert('修改成功，请重新登录').then(()=>{this.$router.push({path:"/"})});
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    },
  },
};
</script>

<style scoped>
#register {
  width: 340px;
  margin: 30px auto;
  background-color: rgba(137, 163, 142, 0.322);
  padding: 20px 40px;
  border-radius: 30px;
  position: static;
  z-index: 9;
  top: 20%;
  right: 30%;
}
.title {
  font-size: 26px;
  line-height: 50px;
  font-weight: bold;
  margin: 10px;
  text-align: center;
  color:rgb(36, 109, 69);
}
.el-form-item {
  text-align: center;
}
.login {
  margin-top: 1px;
  font-size: 14px;
  line-height: 22px;
  color: #1ab2ff;
  cursor: pointer;
  text-align: left;
  text-indent: 8px;
  width: 160px;
}
.login:hover {
  color: #2c2fd6;
}
.code >>> .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.code button {
  margin-left: 20px;
  width: 140px;
  text-align: center;
}
.el-button--primary:focus {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
}
.buttons{
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}
.el-breadcrumb{
    margin-bottom: 15px;
    font-size:15px;
}
</style>